package com.caseProcess.controller;

import com.caseProcess.entity.*;
import com.caseProcess.service.UserService;
import com.caseProcess.service.VUserInfoService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/vUserInfo")
public class VUserInfoController {
    @Resource
    private VUserInfoService vUserInfoService;

    @Resource
    private UserService userService;


    /**
     * 退出
     * @param request
     * @return
     */
    @RequestMapping("/quit")
    public Result quit(HttpServletRequest request) {
        Result result=new Result();
        SessionController.removeAttribute(request,"police");
        result.setMsg("成功");
        return result;
    }

    /**
     * 查询派出所警员
     */
    @CrossOrigin
    @RequestMapping("/selectPolice")
    public Result selectPolice(String departmentId, long grade) {
        VUserInfo user = new VUserInfo();
        user.setDepartmentId(departmentId);
        user.setGrade(grade);
        Result result = new Result();
        try {
            List<VUserInfo> vUserInfoList = vUserInfoService.selectPolice(user);
            if (vUserInfoList.size() >= 0) {
                for(int i=0;i<vUserInfoList.size();i++){
                    vUserInfoList.get(i).setPwd("");
                }
                result.setCode(Result.RESULT_OK);
                result.setMsg("操作成功");
                result.setData(vUserInfoList);
            } else {
                result.setCode(Result.RESULT_ERROR);
                result.setMsg("操作失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 查询派出所警员
     */
    @CrossOrigin
    @RequestMapping("/selectByGrade")
    public Result selectByGrade(long grade) {
        VUserInfo vUserInfo = new VUserInfo();
        vUserInfo.setGrade(grade);
        Result result = new Result();
            try {
            List<VUserInfo> vUserInfoList=vUserInfoService.selectVUserInfosBy(vUserInfo);
            if (vUserInfoList.size() >= 0) {
                for(int i=0;i<vUserInfoList.size();i++){
                    vUserInfoList.get(i).setPwd("");
                }
                result.setCode(Result.RESULT_OK);
                result.setMsg("查询成功");
                result.setData(vUserInfoList);
            } else {
                result.setCode(Result.RESULT_ERROR);
                result.setMsg("未查询到任何数据");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 登陆
     * @param policeId
     * @param password
     * @param request
     * @return
     */
    @RequestMapping("/login")
    public Result login(String policeId, String password, HttpServletRequest request){
        Result result=new Result();
        if(policeId==null||password==null||policeId.isEmpty()||password.isEmpty()){
            result.setMsg("账号或密码错误");
            result.setCode(Result.RESULT_ERROR);
            return result;
        }
        VUserInfo vUserInfo=new VUserInfo();
        vUserInfo.setPoliceId(policeId);
        List<VUserInfo> list=vUserInfoService.selectVUserInfosBy(vUserInfo);
        if(list.isEmpty()){
            result.setMsg("账号或密码错误");
            result.setCode(Result.RESULT_ERROR);
            return result;
        }
        if (list.get(0).getIsLocking() == 5){
            result.setMsg("该账号已被锁,请联系管理员");
            result.setCode(Result.RESULT_ERROR);
            return result;
        }
        if (password.equals(list.get(0).getPwd())){
            userService.upLocking(0,list.get(0).getPoliceId());
            result.setMsg("成功");
            list.get(0).setPwd("");
            result.setData(list.get(0));
            request.getSession().setAttribute("police",list.get(0));
        }else {
            userService.upLocking(list.get(0).getIsLocking()+1,list.get(0).getPoliceId());
            result.setMsg("账号或密码错误");
            result.setCode(Result.RESULT_ERROR);
            result.setData(list.get(0).getIsLocking()+1);
        }
        return result;
    }
}
